/**
 * 
 */
package com.vector.ding.web.service.report;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vector.ding.commons.util.BeanUtil;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.dao.mybatis.mapper.RzdbgsBaseInfoMapper;
import com.vector.ding.dao.mybatis.model.RzdbgsBaseInfo;
import com.vector.ding.dao.mybatis.model.RzdbgsBaseInfoExample;
import com.vector.ding.web.service.bizinfo.RzdbgsBizInfoMgrService;
import com.vector.ding.web.service.common.BaseInfoScoreService;
import com.vector.ding.web.service.common.BearScoreService;
import com.vector.ding.web.service.common.MediaScoreService;

/**
 * @author vector
 *
 */
@Service
public class RzdbReportService {
	
	@Autowired
	private RzdbgsBaseInfoMapper baseMapper;
	@Autowired
	private RzdbgsBizInfoMgrService bizService;
	@Autowired
	private BaseInfoScoreService baseScoreSrevice;
	@Autowired
	private MediaScoreService mediaScoreService;
	@Autowired
	private BearScoreService bearScoreService;
	
	public List<Map<String, Object>> getSearchData(int pageIndex, int pageSize,
			HttpServletRequest request) {
		PageHelper.startPage(pageIndex, pageSize, true);
		RzdbgsBaseInfoExample example = getBaseInfoExample(request);
		List<RzdbgsBaseInfo> baseInfoList = baseMapper.selectByExample(example);
		String belongYearStr = StringUtil.getString(request.getParameter("belong_year"));
		Integer belongYear = null;
		if (StringUtil.isNotEmpty(belongYearStr))
			belongYear = Integer.parseInt(belongYearStr);
		List<Map<String, Object>> searchMapData = getRelateInfo((Page<RzdbgsBaseInfo>) baseInfoList, belongYear);
		
		return searchMapData;
	}
	
	private RzdbgsBaseInfoExample getBaseInfoExample(HttpServletRequest request) {
		RzdbgsBaseInfoExample example = new RzdbgsBaseInfoExample();
		String companyName = StringUtil.getString(request.getParameter("company_name"));
		example.or().andCompanyNameLike(StringUtil.wrapString(companyName, "%", "%"));
		return example;
	}
	
	private List<Map<String, Object>> getRelateInfo(
			Page<RzdbgsBaseInfo> baseInfoList, Integer belongYear) {
		if (CollectionUtil.isEmpty(baseInfoList))
			return Collections.emptyList();
		
		Page<Map<String, Object>> list = new Page<Map<String, Object>>();
		for (RzdbgsBaseInfo baseInfo: baseInfoList) {
			Map<String, Object> mapData = BeanUtil.getPropertyMap(baseInfo);
			String companyName = MapUtils.getString(mapData, "companyName");
			if (belongYear != null)
				addScoreInfo(mapData, companyName, belongYear);
			else
				addScoreInfo(mapData, companyName);
			list.add(mapData);
		}
		list.setTotal(baseInfoList.getTotal());
		return list;
	}
	
	private void addScoreInfo(Map<String, Object> mapData, String companyName) {
		BigDecimal sumScore = new BigDecimal(0);
		BigDecimal bizScore = bizService.getScore(companyName);
		if (bizScore != null) {
			sumScore = sumScore.add(bizScore);
			mapData.put("bizScore", bizScore.toString());
		}
		BigDecimal baseScore = baseScoreSrevice.getScore(companyName);
		if (baseScore != null) {
			sumScore = sumScore.add(baseScore);
			mapData.put("baseScore", baseScore.toString());
		}
		BigDecimal mediaScore = mediaScoreService.getScore(companyName);
		if (mediaScore != null) {
			sumScore = sumScore.add(mediaScore);
			mapData.put("mediaScore", mediaScore.toString());
		}
		BigDecimal bearScore = bearScoreService.getScore(companyName);
		if (bearScore != null) {
			sumScore = sumScore.add(bearScore);
			mapData.put("bearScore", bearScore.toString());
		}
		
		mapData.put("sumScore", sumScore.toString());
	}
	
	private void addScoreInfo(Map<String, Object> mapData, 
			String companyName, Integer belongYear) {
		if (belongYear == null)
			return ;
			
		BigDecimal sumScore = new BigDecimal(0);
		BigDecimal bizScore = bizService.getScore(companyName, belongYear);
		if (bizScore != null) {
			sumScore = sumScore.add(bizScore);
			mapData.put("bizScore", bizScore.toString());
		}
		BigDecimal baseScore = baseScoreSrevice.getScore(companyName, belongYear);
		if (baseScore != null) {
			sumScore = sumScore.add(baseScore);
			mapData.put("baseScore", baseScore.toString());
		}
		BigDecimal mediaScore = mediaScoreService.getScore(companyName, belongYear);
		if (mediaScore != null) {
			sumScore = sumScore.add(mediaScore);
			mapData.put("mediaScore", mediaScore.toString());
		}
		BigDecimal bearScore = bearScoreService.getScore(companyName, belongYear);
		if (bearScore != null) {
			sumScore = sumScore.add(bearScore);
			mapData.put("bearScore", bearScore.toString());
		}
		
		mapData.put("sumScore", sumScore.toString());
	}
	

}
